<template>
  <div id="app">
    <!-- 路由视图 -->
    <router-view />
  </div>
</template>

<script>
import { ref, onMounted, watch } from 'vue';
import { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';

export default {
  name: 'App',
  setup() {
    const router = useRouter();
    
    // 用户登录状态检查
    const checkLoginStatus = () => {
      try {
        const storedUserInfo = localStorage.getItem('userInfo');
        if (!storedUserInfo) {
          // 如果未登录，确保在首页时重定向到登录页
          if (router.currentRoute.value.path !== '/login' && router.currentRoute.value.path !== '/register') {
            router.push('/login');
          }
        } else {
          // 如果已登录且在登录/注册页，重定向到首页
          if (router.currentRoute.value.path === '/login' || router.currentRoute.value.path === '/register') {
            router.push('/');
          }
        }
      } catch (error) {
        console.error('检查登录状态失败:', error);
      }
    };
    
    // 监听路由变化，检查登录状态
    watch(() => router.currentRoute.value.path, () => {
      checkLoginStatus();
    });
    
    // 组件挂载时检查登录状态
    onMounted(() => {
      checkLoginStatus();
    });
    
    return {
      checkLoginStatus
    };
  }
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
  height: 100vh;
  display: flex;
  flex-direction: column;
}
</style>